package com.jason.schedulemigrationtest.service.impl;

import com.jason.schedulemigrationtest.mapper.MigrationArchiveLogMapper;
import com.jason.schedulemigrationtest.po.MigrationArchiveLog;
import com.jason.schedulemigrationtest.service.MigrationArchiveLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 数据迁移归档日志服务实现类
 */
@Service
@Slf4j
public class MigrationArchiveLogServiceImpl implements MigrationArchiveLogService {
    

    @Resource
    private MigrationArchiveLogMapper migrationArchiveLogMapper;
    
    @Override
    public Long recordMigration(String batchId, Long startOrderId, Long endOrderId, int counts, String msg) {

        String status = MigrationArchiveLog.STATUS_SUCCESS;
        if(counts == 0){
            status = MigrationArchiveLog.STATUS_ROLLBACK;
        }

        MigrationArchiveLog archiveLog = new MigrationArchiveLog().builder()
                .batchId(batchId)
                .startOrderId(startOrderId)
                .endOrderId(endOrderId)
                .migrationCount(counts)
                .status(status)
                .migrationTime(LocalDateTime.now())
                .errorMessage(msg)
                .build();
        migrationArchiveLogMapper.insert(archiveLog);
        log.info("迁移日志记录完成 - ID: {}", archiveLog.getId());
        return archiveLog.getId();
    }

    @Override
    public List<MigrationArchiveLog> getByDate(String startDate, String endDate) {
        log.debug("查询归档日志 - 日期: {}", startDate + " - " + endDate);
        return migrationArchiveLogMapper.selectByDate(startDate, endDate);
    }

    @Override
    public List<MigrationArchiveLog> getRecentLogs(int limit) {
        log.debug("查询最近的归档日志记录 - 限制数量: {}", limit);
        return migrationArchiveLogMapper.selectRecentLogs(limit);
    }
    
    @Override
    public List<MigrationArchiveLog> getRollbackLogs() {
        log.debug("查询回滚状态的归档日志");
        return migrationArchiveLogMapper.selectByStatus(MigrationArchiveLog.STATUS_ROLLBACK);
    }

    @Override
    public MigrationArchiveLog getByBatchId(String batchId) {
        return migrationArchiveLogMapper.selectByBatchId(batchId);
    }


}